package com.jair.adb;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.Scanner;

import com.jair.post.httpClientTest;

import sun.misc.ThreadGroupUtils;

public class Test {
	public static void main(String[] args) throws Exception{
		Date date = new Date();
		
		String dateStr = String.valueOf(date.getTime());
		String img_file_path = "D:\\test2"+dateStr+".png";//电脑本地的文件地址
		String commond = "E:\\adb\\adb.exe shell screencap -p /sdcard/screenshot"+dateStr+".png";
		String commond2 = "E:\\adb\\adb.exe pull /sdcard/screenshot"+dateStr+".png D:\\test2"+dateStr+".png";//这个耗时间
		String commond3 = "E:\\adb\\adb.exe shell screencap -p ";
		Process p = Runtime.getRuntime().exec("E:\\adb\\adb.exe shell screencap -p " );
		Runtime.getRuntime().exec(commond);
		Thread.sleep(400);//截图有延时
		Runtime.getRuntime().exec(commond2);
		Thread.sleep(400);//截图有延时
		//截图
		String img_file_path2 = "D:\\test3"+dateStr+".png";//电脑本地的文件地址
		//UploadImg s = new UploadImg(img_file_path, "D:\\", "ps_low2","png",280,280);  
         //System.out.println(s.createThumbnail());  
		cutImage.cutPic(img_file_path,img_file_path2,75,286,985,888);
		//cutImage.cutPic(img_file_path,img_file_path2,0,0,20,20);
		//读取当前图像，进行OCR
		Thread.sleep(400);
		//httpClientTest.postSougo(img_file_path2);
//		InputStream in = p.getInputStream();
//		File f = new File("D://test"+dateStr+".png");
//		inputstreamtofile(in,f);
	}
	
	public static void pull(){//手机到android
        String cmd="E:\\adb\\adb.exe /c shell screencap /sdcard/screensht.png ";
        System.out.println("adb push"+cmd);
        Process p;
        try {
            p = Runtime.getRuntime().exec(cmd);
            System.out.println(":::::::::::::::::::::::::::::::::::::::::>>>>>>");
            p.waitFor();
            Scanner sc = new Scanner(p.getErrorStream());
            if(sc.hasNext())
                System.out.println(sc.next());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
	
	public  static void inputstreamtofile(InputStream ins,File file) throws IOException{  
		   OutputStream os = new FileOutputStream(file);  
		   int bytesRead = 0;  
		   byte[] buffer = new byte[8192];  
		   while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {  
		      os.write(buffer, 0, bytesRead);  
		   }  
		   os.close();  
		   ins.close();  
		} 
	
//	  private byte[] Fix0d0d0a(byte[] bytes)
//	    {
//	        int length = bytes.length;
//	        byte[] bytesfix = new byte[length];
//
//	        int idx = 0;
//	        int count = 0;
//	        int idxFirst0D = 0;
//	        int idxFirst0A = 0;
//	        boolean is0D = false;
//	        for (int i = 0; i < length; i++)
//	        {
//	            byte b = bytes[i];
//	            if (b == (byte)0x0d && idxFirst0D == 0)
//	            {
//	                idxFirst0D = i;
//	                is0D = true;
//	            }
//	            if (b == (byte)0x0a && idxFirst0A == 0)
//	            {
//	                idxFirst0A = i;
//	            }
//	            if (i > 2 && b == (byte)0x0a && is0D)
//	            {
//	                count++;
//	                idx = idx - (idxFirst0A - idxFirst0D - 1);
//	                bytesfix[idx] = b;
//	                idx++;
//	            }
//	            else
//	            {
//	                bytesfix[idx] = b;
//	                idx++;
//	            }
//	            if (b == 0x0d)
//	                is0D = true;
//	            else
//	                is0D = false;
//	        }
//	        byte[] bytesfinal = new byte[length-count* (idxFirst0A - idxFirst0D-1)];
//	        //Buffer.BlockCopy(bytesfix, 0, bytesfinal, 0, bytesfinal.length);           
//	        return bytesfinal;
//	    }
}
